import HttpService from "./request";

export const handle = ({url, params, method,config,filName}: any) => {
  const httpService = new HttpService()
  // @ts-ignore
  return httpService[method.toLowerCase()](url, params,config,filName)
}

// @ts-ignore
const files = import.meta.glob("/src/api/modules/**/*.ts")
const apiModule: any = {}
const apiRepeatMap = new Map()
let num1 = 0
let num2 = 0

for (const [key, value] of Object.entries(files)) {
  num1++
  // @ts-ignore
  value().then(res => {
    for (const [apiKey, apiValue] of Object.entries(res)) {
      if (apiKey !== '__tla') { // 过滤掉内部属性，非业务
        if (apiRepeatMap.has(apiKey)) {
          apiRepeatMap.set(apiKey, [...apiRepeatMap.get(apiKey), key])
        } else {
          apiRepeatMap.set(apiKey, [key])
        }
      }
    }
    Object.assign(apiModule, res)
    num2++
    if (num1 === num2) {
      apiRepeatMap.forEach((v, k) => {
        if (v.length > 1) {
          console.error(`检测到接口${k}重复定义，路径为：`, v)
        }
      })
    }
  })
}

export default apiModule
